package BanyuanClub.Work.Story.Four;

import javax.swing.*;
import java.io.EOFException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ATM {
    Scanner in = new Scanner(System.in);
    BankServer bank = new BankServer();
//欢迎界面
    public void welcomeWindow(){
        System.out.println("----欢迎使用本机----");
        inCardWindow();
    }
//输入卡号界面
    public void inCardWindow(){
        System.out.println("请输入卡号（5位）：");
        String inCode = in.next();
        String myCard = bank.typeInTheCode(inCode);
        if (bank.myAccound(inCode).getAccountFreeze()==false){
            System.out.println("账户已冻结");
            inCardWindow();
        }
        if (myCard == null){
            System.out.println("卡号错误");
            inCardWindow();
        }else {
            inPasswordWindow(inCode);
        }
    }
//输入密码界面
    public void inPasswordWindow(String inCode){
        System.out.println("请输入密码：");
        String passwords = in.next();
        String result = bank.typeInThePassword(inCode,passwords);
        if (bank.myAccound(inCode).getT()==2){
            System.out.println("您的账户已冻结");
            bank.myAccound(inCode).Dong();
            inCardWindow();
        }else if (result==null){
            System.out.println("密码错误");
            bank.myAccound(inCode).setT(bank.myAccound(inCode).getT()+1);
            inPasswordWindow(inCode);
        }else {
            homepageWindow(inCode);
        }

    }
//操作界面
    public void homepageWindow(String inCode){
        System.out.println("请输入指令：1.存款 2.取款 3.查询 4.转账");
        String command = in.next();
        if ("1".equals(command)){
            inMoneyWindow(inCode);
        }else if("2".equals(command)){
            outMongeyWindow(inCode);
        }else if ("3".equals(command)){
            queryBalance(inCode);
        }else if ("4".equals(command)){
            transferWondow(inCode);
        }else {
            System.out.println("请按指示输入");
            homepageWindow(inCode);
        }
    }
//存款界面
    public void inMoneyWindow(String inCode){
        System.out.println("请输入存款金额(需为整百且不大于5000)");
        String inMoney = in.next();
        int money = Integer.parseInt(inMoney);
        String s = bank.inMoney(money,inCode);
        if (s == null){
            System.out.println("请按提示操作");
            inMoneyWindow(inCode);
        }else if (s.equals("存款成功")) {
            System.out.println(s);
            System.out.println("是否打印凭条（y/n）");
            String p = in.next();
            if (p.equals("y")){
                System.out.println("账号："+inCode);
                System.out.println("存款时间："+time());
                System.out.println("存款金额："+money);
            }else if (p.equals("n")) {
                homepageWindow(inCode);
            }
        }
        Exit(inCode);
    }
//取款界面
    public void outMongeyWindow(String inCode){
        System.out.println("请输入取款金额(需为整百且不大于5000)");
        String outMoney = in.next();
        int money = Integer.parseInt(outMoney);
        String s = bank.outMoney(money,inCode);
        if (s == null){
            System.out.println("请按提示操作");
            outMongeyWindow(inCode);
        } else if (s.equals("余额不足")){
                 System.out.println(s);
        } else if (s.equals("请收好你的钱")){
            System.out.println(s+money);
            System.out.println("是否打印凭条（y/n）");
            String p = in.next();
            if (p.equals("y")){
                System.out.println("账号："+inCode);
                System.out.println("取款时间："+time());
                System.out.println("取款金额："+money);
            }else if (p.equals("n")) {
                homepageWindow(inCode);
            }
        }
        Exit(inCode);
    }
//查询界面
    public void queryBalance(String inCode){
        bank.query(inCode);
        Exit(inCode);
    }
//转账界面
    public void transferWondow(String inCode){
        System.out.println("请输入转账对方卡号,转账金额，密码");
        String inYourCode = in.next();
        String money1 = in.next();
        String inCodePassword = in.next();
        int money2 = Integer.parseInt(money1);
        String result = bank.yourCode(inYourCode,money2,inCodePassword,inCode);
        if (result.equals("转账成功")){
            System.out.println("成功转账："+money2);
            System.out.println("是否打印凭条（y/n）");
            String p = in.next();
            if (p.equals("y")){
                System.out.println("账号："+inCode);
                System.out.println("对方账号："+inYourCode);
                System.out.println("转账时间："+time());
                System.out.println("转账金额："+money2);
            }else if (p.equals("n")) {
                homepageWindow(inCode);
            }
            Exit(inCode);
        }else {
            transferWondow(inCode);
        }
    }
//是否返回主页
    public void Exit(String inCode){
        System.out.println("是否返回主页 yes or no (y/n)");
        String s = in.next();
        if ("y".equals(s)){
            homepageWindow(inCode);
        }else {
            welcomeWindow();
        }
    }

//时间
    public String time(){
        Date D = new Date();
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sp.format(D);
        return s;
    }

}
